<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定时任务触发管理</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .status-card {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
        }
        .status-indicator {
            display: inline-block;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            margin-right: 8px;
        }
        .status-running {
            background-color: #28a745;
        }
        .status-stopped {
            background-color: #dc3545;
        }
        .button-group {
            display: flex;
            gap: 15px;
            margin-bottom: 20px;
        }
        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        .btn-primary {
            background-color: #007bff;
            color: white;
        }
        .btn-primary:hover {
            background-color: #0056b3;
        }
        .btn-danger {
            background-color: #dc3545;
            color: white;
        }
        .btn-danger:hover {
            background-color: #c82333;
        }
        .btn-secondary {
            background-color: #6c757d;
            color: white;
        }
        .btn-secondary:hover {
            background-color: #545b62;
        }
        .btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }
        .info-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
            margin-top: 15px;
        }
        .info-item {
            background: white;
            padding: 15px;
            border-radius: 6px;
            border: 1px solid #e9ecef;
        }
        .info-label {
            font-weight: 600;
            color: #495057;
            margin-bottom: 5px;
        }
        .info-value {
            color: #212529;
        }
        .message {
            padding: 15px;
            border-radius: 6px;
            margin-bottom: 20px;
            display: none;
        }
        .message.success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .message.error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>定时任务触发管理</h1>
        
        <div id="message" class="message"></div>
        
        <div class="status-card">
            <h3>当前状态</h3>
            <div id="statusDisplay">
                <span class="status-indicator status-stopped"></span>
                <span id="statusText">正在获取状态...</span>
            </div>
            <div class="info-grid">
                <div class="info-item">
                    <div class="info-label">启动时间</div>
                    <div class="info-value" id="startTime">-</div>
                </div>
                <div class="info-item">
                    <div class="info-label">最后更新时间</div>
                    <div class="info-value" id="lastUpdateTime">-</div>
                </div>
            </div>
        </div>
        
        <div class="button-group">
            <button class="btn btn-primary" onclick="startJobTrigger()" id="startBtn">启动处理器</button>
            <button class="btn btn-danger" onclick="stopJobTrigger()" id="stopBtn">停止处理器</button>
            <button class="btn btn-secondary" onclick="refreshStatus()">刷新状态</button>
        </div>
    </div>

    <script>
        const API_BASE = '/api/job-trigger';
        
        // 显示消息
        function showMessage(message, type = 'success') {
            const messageDiv = document.getElementById('message');
            messageDiv.textContent = message;
            messageDiv.className = `message ${type}`;
            messageDiv.style.display = 'block';
            
            setTimeout(() => {
                messageDiv.style.display = 'none';
            }, 5000);
        }
        
        // 更新状态显示
        function updateStatusDisplay(status) {
            const statusIndicator = document.querySelector('.status-indicator');
            const statusText = document.getElementById('statusText');
            const startTime = document.getElementById('startTime');
            const lastUpdateTime = document.getElementById('lastUpdateTime');
            const startBtn = document.getElementById('startBtn');
            const stopBtn = document.getElementById('stopBtn');
            
            if (status.running) {
                statusIndicator.className = 'status-indicator status-running';
                statusText.textContent = status.statusDescription;
                startBtn.disabled = true;
                stopBtn.disabled = false;
            } else {
                statusIndicator.className = 'status-indicator status-stopped';
                statusText.textContent = status.statusDescription;
                startBtn.disabled = false;
                stopBtn.disabled = true;
            }
            
            startTime.textContent = status.startTime;
            lastUpdateTime.textContent = status.lastUpdateTime;
        }
        
        // 获取状态
        async function refreshStatus() {
            try {
                const response = await fetch(`${API_BASE}/status`);
                const result = await response.json();
                
                if (result.success) {
                    updateStatusDisplay(result.data);
                } else {
                    showMessage('获取状态失败: ' + result.message, 'error');
                }
            } catch (error) {
                showMessage('获取状态失败: ' + error.message, 'error');
            }
        }
        
        // 启动处理器
        async function startJobTrigger() {
            try {
                const response = await fetch(`${API_BASE}/start`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });
                const result = await response.json();
                
                if (result.success) {
                    showMessage(result.data);
                    refreshStatus();
                } else {
                    showMessage('启动失败: ' + result.message, 'error');
                }
            } catch (error) {
                showMessage('启动失败: ' + error.message, 'error');
            }
        }
        
        // 停止处理器
        async function stopJobTrigger() {
            try {
                const response = await fetch(`${API_BASE}/stop`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });
                const result = await response.json();
                
                if (result.success) {
                    showMessage(result.data);
                    refreshStatus();
                } else {
                    showMessage('停止失败: ' + result.message, 'error');
                }
            } catch (error) {
                showMessage('停止失败: ' + error.message, 'error');
            }
        }
        
        // 页面加载时获取状态
        document.addEventListener('DOMContentLoaded', function() {
            refreshStatus();
        });
    </script>
</body>
</html>
